home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / lib / python2.5 / contextlib.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-29  |  4KB  |  175 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''Utilities for with-statement contexts.  See PEP 343.'''
  5. import sys
  6. __all__ = [
  7.     'contextmanager',
  8.     'nested',
  9.     'closing']
  10.  
  11. class GeneratorContextManager(object):
  12.     '''Helper for @contextmanager decorator.'''
  13.     
  14.     def __init__(self, gen):
  15.         self.gen = gen
  16.  
  17.     
  18.     def __enter__(self):
  19.         
  20.         try:
  21.             return self.gen.next()
  22.         except StopIteration:
  23.             raise RuntimeError("generator didn't yield")
  24.  
  25.  
  26.     
  27.     def __exit__(self, type, value, traceback):
  28.         if type is None:
  29.             
  30.             try:
  31.                 self.gen.next()
  32.             except StopIteration:
  33.                 return None
  34.  
  35.             raise RuntimeError("generator didn't stop")
  36.         elif value is None:
  37.             value = type()
  38.         
  39.         
  40.         try:
  41.             self.gen.throw(type, value, traceback)
  42.             raise RuntimeError("generator didn't stop after throw()")
  43.         except StopIteration:
  44.             exc = None
  45.             return exc is not value
  46.         except:
  47.             if sys.exc_info()[1] is not value:
  48.                 raise 
  49.             
  50.  
  51.  
  52.  
  53.  
  54. def contextmanager(func):
  55.     '''@contextmanager decorator.
  56.  
  57.     Typical usage:
  58.  
  59.         @contextmanager
  60.         def some_generator(<arguments>):
  61.             <setup>
  62.             try:
  63.                 yield <value>
  64.             finally:
  65.                 <cleanup>
  66.  
  67.     This makes this:
  68.  
  69.         with some_generator(<arguments>) as <variable>:
  70.             <body>
  71.  
  72.     equivalent to this:
  73.  
  74.         <setup>
  75.         try:
  76.             <variable> = <value>
  77.             <body>
  78.         finally:
  79.             <cleanup>
  80.  
  81.     '''
  82.     
  83.     def helper(*args, **kwds):
  84.         return GeneratorContextManager(func(*args, **kwds))
  85.  
  86.     
  87.     try:
  88.         helper.__name__ = func.__name__
  89.         helper.__doc__ = func.__doc__
  90.         helper.__dict__ = func.__dict__
  91.     except:
  92.         (None,)
  93.  
  94.     return helper
  95.  
  96.  
  97. def nested(*managers):
  98.     '''Support multiple context managers in a single with-statement.
  99.  
  100.     Code like this:
  101.  
  102.         with nested(A, B, C) as (X, Y, Z):
  103.             <body>
  104.  
  105.     is equivalent to this:
  106.  
  107.         with A as X:
  108.             with B as Y:
  109.                 with C as Z:
  110.                     <body>
  111.  
  112.     '''
  113.     exits = []
  114.     vars = []
  115.     exc = (None, None, None)
  116.     
  117.     try:
  118.         for mgr in managers:
  119.             exit = mgr.__exit__
  120.             enter = mgr.__enter__
  121.             vars.append(enter())
  122.             exits.append(exit)
  123.         
  124.         yield vars
  125.     except:
  126.         exc = sys.exc_info()
  127.     finally:
  128.         while exits:
  129.             exit = exits.pop()
  130.             
  131.             try:
  132.                 if exit(*exc):
  133.                     exc = (None, None, None)
  134.             continue
  135.             exc = sys.exc_info()
  136.             continue
  137.  
  138.         if exc != (None, None, None):
  139.             raise exc[0], exc[1], exc[2]
  140.         
  141.  
  142.  
  143. nested = contextmanager(nested)
  144.  
  145. class closing(object):
  146.     '''Context to automatically close something at the end of a block.
  147.  
  148.     Code like this:
  149.  
  150.         with closing(<module>.open(<arguments>)) as f:
  151.             <block>
  152.  
  153.     is equivalent to this:
  154.  
  155.         f = <module>.open(<arguments>)
  156.         try:
  157.             <block>
  158.         finally:
  159.             f.close()
  160.  
  161.     '''
  162.     
  163.     def __init__(self, thing):
  164.         self.thing = thing
  165.  
  166.     
  167.     def __enter__(self):
  168.         return self.thing
  169.  
  170.     
  171.     def __exit__(self, *exc_info):
  172.         self.thing.close()
  173.  
  174.  
  175.